package com.hsck.order.kds.main.fragment.standard.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.databinding.ComboDialogItemLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

class ComboItemAdapter : RecyclerView.Adapter<ComboItemAdapter.ItemVH>() {

    private val diffUtil = object : DiffUtil.ItemCallback<TestListBean>() {
        override fun areItemsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: TestListBean, newItem: TestListBean): Boolean {
            return oldItem == newItem
        }
    }
    private val asyncListDiffer = AsyncListDiffer(this, diffUtil)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            ComboDialogItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    inner class ItemVH(val binding: ComboDialogItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(position: Int) {
            val itemBean = asyncListDiffer.currentList[position]
            binding.apply {
                if (position % 2 == 0) {
                    binding.tvDishRemark.visibility = View.GONE
                }
            }
        }
    }

    override fun onBindViewHolder(holder: ItemVH, position: Int) {
        holder.bindData(position)
    }

    fun submitList(dataResponse: List<TestListBean>) {
        asyncListDiffer.submitList(dataResponse)
    }

    override fun getItemCount(): Int {
        return asyncListDiffer.currentList.size
    }
}
